package io.gitee.chearnee.fw.datasecurity.core.handler;


import io.gitee.chearnee.fw.datasecurity.core.annotation.CryptField;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 处理 List 对象的加解密
 *
 * @author kamjin1996
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class ListCryptHandler implements CryptHandler<List> {

    @Override
    public Object encrypt(List list, CryptField cryptField) {
        if (ignoreCrypt(list)) {
            return list;
        }
        return list.stream()
                .map(item -> CryptHandlerFactory.getCryptHandler(item, cryptField).encrypt(item, cryptField))
                .collect(Collectors.toList());
    }

    @Override
    public Object decrypt(List param, CryptField cryptField) {
        if (ignoreCrypt(param)) {
            return param;
        }
        return param.stream()
                .map(item -> CryptHandlerFactory.getCryptHandler(item, cryptField).decrypt(item, cryptField))
                .collect(Collectors.toList());
    }

    private boolean ignoreCrypt(List list) {
        return list == null || list.isEmpty();
    }
}
